﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Phoenix.AppCode.Data;

namespace Phoenix.Backend
{
    public partial class Article : System.Web.UI.Page
    {
        protected DataTable dt;
        DataTable dtArticleCategory;
        Phoenix.AppCode.Entities.Article article = new Phoenix.AppCode.Entities.Article();
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'>" + "Cập nhật thông tin thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'>" + "Thông tin chưa được cập nhật." + "</p></div><br  class='clear'/>";
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    // load drop down list article type
                    dtArticleCategory = Phoenix.AppCode.Bussiness.ArticleService.ArticleCategoryList(Request.Cookies["Lang_Backend"].Value);

                    ddlArticleCategory.DataSource = dtArticleCategory;
                    ddlArticleCategory.DataTextField = "Name";
                    ddlArticleCategory.DataValueField = "ID_ArticleCategory";
                    ddlArticleCategory.DataBind();
                    //add select dropdownlist
                    ddlArticleCategory.Items.Insert(0, new ListItem("Xem tất cả", "0"));

                    if (Request.Cookies["ddlArticleCategory"] == null)
                    {
                        Response.Cookies["ddlArticleCategory"].Value = "0";
                        Response.Cookies["ddlArticleCategory"].Expires = DateTime.Now.AddDays(1);
                    }
                    dt = Phoenix.AppCode.Bussiness.ArticleService.spArticleListByIdCat(Convert.ToInt32(Request.Cookies["ddlArticleCategory"].Value.Trim()), Request.Cookies["Lang_Backend"].Value);
                    
                    ddlArticleCategory.SelectedValue = Request.Cookies["ddlArticleCategory"].Value;

                    if (Request.QueryString["Action"] == "Create" || Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "OnOff" || Request.QueryString["Action"] == "Delete")
                    {
                        add.Visible = false;
                        edit.Visible = false;
                        Load_ActionArticle(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        #region  Load action article
        private void Load_ActionArticle(string action)
        {
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Create")
                {
                    add.Visible = true;
                    edit.Visible = false;
                    list.Visible = false;

                    // load drop down list article type
                    dtArticleCategory = Phoenix.AppCode.Bussiness.ArticleService.ArticleCategoryList(Request.Cookies["Lang_Backend"].Value);

                    ddlArticleCategoryA.DataSource = dtArticleCategory;
                    ddlArticleCategoryA.DataTextField = "Name";
                    ddlArticleCategoryA.DataValueField = "ID_ArticleCategory";
                    ddlArticleCategoryA.DataBind();

                    ddlArticleCategoryA.SelectedValue = Request.Cookies["ddlArticleCategory"].Value;
                }
                else if (action == "Edit")
                {
                    add.Visible = false;
                    edit.Visible = true;
                    list.Visible = false;

                    // load drop down list article type
                    dtArticleCategory = Phoenix.AppCode.Bussiness.ArticleService.ArticleCategoryList(Request.Cookies["Lang_Backend"].Value);

                    ddlArticleCategoryE.DataSource = dtArticleCategory;
                    ddlArticleCategoryE.DataTextField = "Name";
                    ddlArticleCategoryE.DataValueField = "ID_ArticleCategory";
                    ddlArticleCategoryE.DataBind();

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.ArticleService.ArticleList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    ddlArticleCategoryE.SelectedValue = dt_Edit.Rows[0]["ID_ArticleCategory"].ToString();
                    txtTitleNameE_vi.Text = dt_Edit.Rows[0]["TitleName"].ToString();
                    txtTitleNameE_en.Text = dt_Edit.Rows[1]["TitleName"].ToString();
                    ckeContentE_vi.Text = dt_Edit.Rows[0]["Content"].ToString();
                    ckeContentE_en.Text = dt_Edit.Rows[1]["Content"].ToString();
                    
                }
                else if (action == "Delete")
                {
                    try
                    {
                        article = Phoenix.AppCode.Bussiness.ArticleService.GetArticleByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.ArticleService.Delete(article);
                        lblWarning.Text = success;
                        //Response.Redirect("Article.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else if (action == "OnOff")
                {
                    try
                    {
                        article = Phoenix.AppCode.Bussiness.ArticleService.GetArticleByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.ArticleService.OnOff(article);
                        lblWarning.Text = success;
                        //Response.Redirect("Article.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else
                {
                    add.Visible = false;
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (ckeContentA_vi.Text != "" && ckeContentA_en.Text != "")
                {
                    article.ID_ArticleCategory = Convert.ToInt32(ddlArticleCategoryA.SelectedValue);
                    article.TitleName_vi = txtTitleNameA_vi.Text;
                    article.TitleName_en = txtTitleNameA_en.Text;
                    article.Content_vi = ckeContentA_vi.Text;
                    article.Content_en = ckeContentA_en.Text;
                    article.PostUser = Session["UserName"].ToString();
                    article.EditUser = Session["UserName"].ToString();
                    article.TitleHead_vi = txtTitleNameA_vi.Text;
                    article.TitleHead_en = txtTitleNameA_en.Text;
                    article.DescriptionHead_vi = txtTitleNameA_vi.Text;
                    article.DescriptionHead_en = txtTitleNameA_vi.Text;
                    article.KeywordHead_vi = txtTitleNameA_vi.Text;
                    article.KeywordHead_en = txtTitleNameA_en.Text;
                    article.UrlHead_vi = Util.ReplaceChar(Util.Filter(txtTitleNameA_vi.Text));
                    article.UrlHead_en = Util.ReplaceChar(Util.Filter(txtTitleNameA_en.Text));
                    Phoenix.AppCode.Bussiness.ArticleService.Create(article);

                    ClearAllFields();
                    lblWarningA.Text = success;
                    //Response.Redirect("Article.aspx?Action=Create");
                }
                else {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language='javascript'>alert('Nội dung không được bỏ trống.')</script>");
                }
            }
            catch
            {
                lblWarningA.Text = error;
                //Response.Redirect("Error.aspx");
            }
        }
        protected void btnCancelA_Click(object sender, EventArgs e)
        {
            ClearAllFields();
            lblWarningA.Text = info;
        }

        private void ClearAllFields()
        {

            txtTitleNameA_vi.Text = "";
            txtTitleNameA_en.Text = "";
            ckeContentA_vi.Text = "";
            ckeContentA_en.Text = "";

            txtTitleNameE_vi.Text = "";
            txtTitleNameE_en.Text = "";
            ckeContentE_vi.Text = "";
            ckeContentE_en.Text = "";
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (ckeContentE_vi.Text != "" && ckeContentE_en.Text != "")
            {
                article = Phoenix.AppCode.Bussiness.ArticleService.GetArticleByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                article.ID_ArticleCategory = Convert.ToInt32(ddlArticleCategoryE.SelectedValue);
                article.TitleName_vi = txtTitleNameE_vi.Text;
                article.TitleName_en = txtTitleNameE_en.Text;
                article.Content_vi = ckeContentE_vi.Text;
                article.Content_en = ckeContentE_en.Text;
                article.EditUser = Session["UserName"].ToString();
                article.TitleHead_vi = txtTitleNameE_vi.Text;
                article.TitleHead_en = txtTitleNameE_en.Text;
                article.DescriptionHead_vi = txtTitleNameE_vi.Text;
                article.DescriptionHead_en = txtTitleNameE_en.Text;
                article.KeywordHead_vi = txtTitleNameE_vi.Text;
                article.KeywordHead_en = txtTitleNameE_en.Text;
                article.UrlHead_vi = txtTitleNameE_vi.Text;
                article.UrlHead_en = txtTitleNameE_en.Text;
                Phoenix.AppCode.Bussiness.ArticleService.Edit(article);
                lblWarningE.Text=success;
                //Response.Redirect("Article.aspx?Action=Edit&ID=" + Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()) + "");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language='javascript'>alert('Nội dung không được bỏ trống.')</script>");
            }
        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.ArticleService.ArticleList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            ddlArticleCategoryE.SelectedValue = dt_Edit.Rows[0]["ID_ArticleCategory"].ToString();
            txtTitleNameE_vi.Text = dt_Edit.Rows[0]["TitleName"].ToString();
            txtTitleNameE_en.Text = dt_Edit.Rows[1]["TitleName"].ToString();
            ckeContentE_vi.Text = dt_Edit.Rows[0]["Content"].ToString();
            ckeContentE_en.Text = dt_Edit.Rows[1]["Content"].ToString();
            lblWarningE.Text = info;
        }

        protected void ddlArticleCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = Phoenix.AppCode.Bussiness.ArticleService.spArticleListByIdCat(Convert.ToInt32(ddlArticleCategory.SelectedValue), Request.Cookies["Lang_Backend"].Value);
            Response.Cookies["ddlArticleCategory"].Value = ddlArticleCategory.SelectedValue;
        }
    }
}